package com.car.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@SuppressWarnings("unchecked")
public class TestList {//koukouGroup two one seven seven seven one two有不明白的进来问
    public static List<Map> turn2Union(List<Map> inList){
        List<Map> outList = new ArrayList<Map>();//用于返回list
        Map<String,Integer> danMaps = new HashMap<String,Integer>();//用于记录单位和金额如果金额不是整数就把Integer改成double啥的
        List<String> danKeys = new ArrayList<String>();//用于记录单位
         
         
        for (int i = 0; i < inList.size(); i++) {
            Map inMap = inList.get(i);
            String oneDan = inMap.get("danwei").toString();//取出单位
            if(danMaps.containsKey(oneDan)){
                danMaps.put(oneDan, danMaps.get(oneDan)+Integer.valueOf(inMap.get("jine").toString()));
            }else{
                danMaps.put(oneDan, Integer.valueOf(inMap.get("jine").toString()));
                danKeys.add(oneDan);
            }
        }
         
        for (int i = 0; i < danKeys.size(); i++) {
            Map map = new HashMap();
            map.put("danwei",danKeys.get(i));
            map.put("jine",danMaps.get(danKeys.get(i)));
            outList.add(map);
        }
        return outList;
    }
     
    public static void main(String[] args) {
        List<Map> list = new ArrayList<Map>();
         
        Map map1 = new HashMap();
        map1.put("danwei", "美元");
        map1.put("jine", "1000");
        list.add(map1);
         
        Map map2 = new HashMap();
        map2.put("danwei", "日元");
        map2.put("jine", "2000");
        list.add(map2);
         
        Map map3 = new HashMap();
        map3.put("danwei", "日元");
        map3.put("jine", "1000");
        list.add(map3);
         
        Map map4 = new HashMap();
        map4.put("danwei", "欧元");
        map4.put("jine", "1500");
        list.add(map4);
         
        List<Map> tList = turn2Union(list);
         
        for (int i = 0; i < tList.size(); i++) {
            Map map = tList.get(i);
            System.out.println(map.get("danwei")+":"+map.get("jine"));
        }
       
    }
}